// component/card/chooseCard.js
const cache = require("../../utils/cache")
const api = require("../../utils/api")
Component({
  options: {
    //addGlobalClass: true
    styleIsolation: 'isolated'
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    patientList: [], // 所有就诊人信息
    currentPatient: '', // 当前就诊人
    count: 8, //可以添加多少人
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleFitCrt(e){
      let that = this
      api.fitCrtPatient(e.currentTarget.dataset.iono).then(res=>{
        cache.setValue("patient",res)
        that.callbackFunc(()=>that.loadData())
        //进行回调 设置完界面可以加载主界面的信息
        that.triggerEvent('callbackLoad',null)
      })
    },
    handleUnbind(e){
      let that = this
      api.unBindPatient(e.currentTarget.dataset.iono).then(res=>{
        cache.setValue("patient",res)
        that.callbackFunc(()=>that.loadData())
      })
    },
    callbackFunc(callback){
      if(callback){
        callback();
      }
    },
    loadData() {
      // 从缓存获取信息
      this.data.patientList.splice(0,this.data.patientList.length);
      let cachePtList = cache.getValue("patient");
      if (cachePtList != undefined && cachePtList != null && cachePtList.length > 0) {
        cachePtList.forEach(m => {
          if (m.crtFlag == "Y") {
            this.data.currentPatient = m;
          } else {
            this.data.patientList.push(m)
          }
        })
        
        this.setData({
          count: 8 - cachePtList.length,
          currentPatient: this.data.currentPatient,
          patientList: this.data.patientList
        })
      }
      this.loadImage(null);
    },
    loadImage(data){
      let userInfo = cache.getValue("scope.userInfo");
      if (userInfo != undefined && userInfo != null && userInfo != '') {
        this.setData({
          image: userInfo.avatarUrl
        })
      }
    },
    handleCreateCard(event) { //创建就诊卡
      wx.navigateTo({
        url: '/pages/user/createCard',
      })
    },
    handleAddCard(event) { //绑定就诊卡
      wx.navigateTo({
        url: '/pages/user/bindCard',
      })
    },
    showModal(e) {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    },
    hideModal(e) {
      this.setData({
        modalName: null
      })
    },
    // ListTouch触摸开始
    ListTouchStart(e) {
      this.setData({
        ListTouchStart: e.touches[0].pageX
      })
    },

    // ListTouch计算方向
    ListTouchMove(e) {
      this.setData({
        ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
      })
    },

    // ListTouch计算滚动
    ListTouchEnd(e) {
      if (this.data.ListTouchDirection == 'left') {
        this.setData({
          modalName: e.currentTarget.dataset.target
        })
      } else {
        this.setData({
          modalName: null
        })
      }
      this.setData({
        ListTouchDirection: null
      })
    },
  },
  lifetimes:{
    attached: function(){
      //this.callbackFunc(()=>this.loadData())
    }
  }
})